package com.gitee.cirnochat.notification.service.impl;

import com.gitee.cirnochat.notification.model.dto.NotificationDTO;
import com.gitee.cirnochat.notification.model.entity.JiguangPushToken;
import com.gitee.cirnochat.notification.service.JiguangPushTokenService;
import com.gitee.cirnochat.notification.service.NotificationService;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
@ConditionalOnProperty(name = "notification.client.type", havingValue = "jiguang")
public class JiguangNotificationServiceImpl implements NotificationService {
    private final JiguangPushTokenService jiguangPushTokenService;

    @Override
    @Async
    public void sendPushNotificationAsync(NotificationDTO pushNotification, String deviceId, Long userId) {
        JiguangPushToken token = jiguangPushTokenService.getJPushTokenByDeviceIdAndUserId(deviceId, userId);
        if (token == null) {
            return; // 如果没有找到对应的推送令牌，则不发送通知
        }
        pushNotification.setTo(List.of(token.getRegisterId()));
    }
}
